package cool.lichuan.zabbix.config;

import cool.lichuan.zabbix.service.ZabbixApiService;
import cool.lichuan.zabbix.service.impl.ZabbixApiServiceImpl;
import org.apache.http.util.Asserts;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author LiChuan
 * @date 2021/11/4 8:49
 */
@Configuration
@ConditionalOnClass(ZabbixProperties.class)
@EnableConfigurationProperties(ZabbixProperties.class)
public class ZabbixAutoConfiguration {
    @Resource
    private ZabbixProperties zabbixProperties;

    @Bean
    @ConditionalOnMissingBean
    public ZabbixApiService zabbixService() {
        int minTime = 600;
        Asserts.check(null != zabbixProperties, "zabbix配置获取失败，请检查配置！");
        Asserts.notEmpty(zabbixProperties.getUrl(), "zabbix请求路径：spring.zabbix.url");
        Asserts.notEmpty(zabbixProperties.getUserName(), "zabbix用户名：spring.zabbix.userName");
        Asserts.notEmpty(zabbixProperties.getPassword(), "zabbix密码：spring.zabbix.password");
        Asserts.check(zabbixProperties.getAutoLogout() >= 90L, "刷新时间不得小于90s");
        if(zabbixProperties.getAutoLogout() < minTime) {
            System.err.println("自动注销时间：" + zabbixProperties.getAutoLogout() + "s太短，建议设置长一些。" +
                    "设置方式：打开zabbix web端，左下角User settings -> 自动注销 例如：90s、5m、2h、1d单位不是秒");
        }
        return new ZabbixApiServiceImpl();
    }
}
